package com.skd.mall.auth_server.controller;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static com.alipay.api.AlipayConstants.APP_ID;
import static com.alipay.api.AlipayConstants.CHARSET;

@Controller
public class OAuthController {

    @RequestMapping("/aliPay/success")
    public String aliPay(@RequestParam("auth_code") String auth_code){
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                "2021004175615045",
                "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDvLaBVQCPPp3PXU9NOrSOZKXgEppqk/liniO4SlOcpV1yczQI4CLjkDeYWKYC5QdQdAdCDIrkkSsuIuNNUG/ZL87p5RIl0ZgIWrYqImgloVrGe30jkO976O7qdzfcpxbc847kI+pNKY0ZU4/tDnXurmyRdCV25dt8tyI+rrHbTgIDEpNNnx+uKQl2SxuMA1f88bqWXuaYznXALXxASn1kcLtrLzsJTh3ZwwQW+SJoOct6tOYWZKckoN0OqUkGAAIfPLG5jDkxQO3oE1+7tjm96n5p6DO6vxmNIoGMRXHthCjsed+ieW+QeVbYn/QttqIkCW795/adHgbfyku3souFtAgMBAAECggEAW1FyS9pHUfs/MVFh9kIIu2un1S6/EIvD0YNoUlPjxlcClyN222oJHZlo5cFG0nSdxc1xjP/u7DpaZ4MZvJ1IdktCQRa33GcuGPZ9VBZrzRh8jIn3zJxHUAcKA0ImQh6pDWhst07keZxZuAOrZW91TlnXkmYHEM6f+1z97a/VVn6Wqo4odsZzHzcLK2jeUdy1T8pJPoP/idq/MPE9BcyvfN1MGQqMsykPIXTV/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QWUCtOswl1sfAupVBHRAyTwe3J1vsxxWCsA14YDxEBCJ157rH7/9S2n5xzCSNzz4NDrm7h6w7DKEkro0Z28jqViBZ9a0os/G5Fd5GvuoDAXYgvgdbHaGOaC7S+Z8zOMtGxDCRHUdaRktyd6BG+4jxRdiEGUBTNDoOVFrBsmh1VUpi4IIX+2XjfUFIuPU6qp0MOdEVhw+CZDIpF8ftXgylGiinelpSxWmUFz91bbmEC11uQvtXfvlJwi8uT0ErZBrYnxqTt/qFTY3zG/D+ZAMYyGMuBqLr11P8PHGJJWBE/PCpVySHuFyxJtDx28p/+/EQ5dUCaWzGOTQIDAQAB",
                "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode(auth_code);
        request.setGrantType("authorization_code");
        try {
            AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
            String accessToken = oauthTokenResponse.getAccessToken();
            AlipayUserInfoShareRequest userInfoShareRequest = new AlipayUserInfoShareRequest();
            AlipayUserInfoShareResponse response = alipayClient.execute(userInfoShareRequest,accessToken);
            String s = JSON.toJSONString(response);
            System.out.println(s);
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }
        return "redirect:http://mall.skd.com";
        //return null;
    }


}

